package servlet;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet("/findById")
public class ShowModifyEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8", "root", "root");
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = ps.executeQuery();
            if (rs.next()) {
                out.println("<!DOCTYPE html>");
                out.println("<html lang=\"en\">");
                out.println("<head>");
                out.println("    <meta charset=\"UTF-8\">");
                out.println("    <title>修改员工信息</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>修改员工信息</h1>");
                out.println("<a href=\"" + req.getContextPath() + "/findAll\">返回员工列表</a>");
                out.println("<hr>");
                out.println("<form action=\""+req.getContextPath()+"/modifyEmp\" method=\"post\">");
                out.println("    编号:<input type=\"text\" name=\"id\" value=\"" + rs.getInt("id") + "\" readonly><br>");
                out.println("    姓名:<input type=\"text\" name=\"name\" value=\"" + rs.getString("name") + "\"><br>");
                out.println("    工资:<input type=\"text\" name=\"salary\" value=\"" + rs.getDouble("salary") + "\"><br>");
                out.println("    性别:");
                out.println("    <input type=\"radio\" name=\"gender\" value=\"man\" " + ("man".equals(rs.getString("gender")) ? "checked" : "") + ">男");
                out.println("    <input type=\"radio\" name=\"gender\" value=\"woman\" " + ("woman".equals(rs.getString("gender")) ? "checked" : "") + ">女<br>");
                out.println("    学历:");
                out.println("    <select name=\"edu\">");
                out.println("        <option value=\"-1\">请选择</option>");
                out.println("        <option value=\"1\" " + ("1".equals(rs.getString("edu")) ? "selected" : "") + ">大专</option>");
                out.println("        <option value=\"2\" " + ("2".equals(rs.getString("edu")) ? "selected" : "") + ">本科</option>");
                out.println("        <option value=\"3\" " + ("3".equals(rs.getString("edu")) ? "selected" : "") + ">研究生</option>");
                out.println("    </select><br>");
                out.println("    兴趣:");
                out.println("    <input type=\"checkbox\" name=\"hobbies\" value=\"eat\" " + StringUtil.checkHobby(rs.getString("hobbies"), "eat") + ">吃饭");
                out.println("    <input type=\"checkbox\" name=\"hobbies\" value=\"sleep\" " + StringUtil.checkHobby(rs.getString("hobbies"), "sleep") + ">睡觉");
                out.println("    <input type=\"checkbox\" name=\"hobbies\" value=\"beat\" " + StringUtil.checkHobby(rs.getString("hobbies"), "beat") + ">打豆豆<br>");
                out.println("    <input type=\"submit\" value=\"修改\">");
                out.println("</form>");
                out.println("</body>");
                out.println("</html>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
